﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using StoreClientModule;
using uCoreClasses;
using MBB_Store_Sales_Report;
using UploadManager;

namespace MBBStoreClient
{
    public partial class frmMain : Form
    {
        
        public frmMain()
        {
            InitializeComponent();

            this.Resize += new EventHandler(frmMain_Resize);
            this.Load += new EventHandler(frmMain_Load);
            this.FormClosing += new FormClosingEventHandler(frmMain_FormClosing);

            //Menu
            
            mnuSettings.Click += new EventHandler(mnuSettings_Click);
            mnuCloseAll.Click += new EventHandler(mnuCloseAll_Click);
            mnuExit.Click += new EventHandler(mnuExit_Click);
            
            mnuViewPriceList.Click += new EventHandler(mnuViewPriceList_Click);
            mnuDailySales.Click += new EventHandler(mnuDailySales_Click);
            mnuAboutThis.Click += new EventHandler(mnuAboutThis_Click);
            
            
            mnuCashReg.Click += new EventHandler(mnuCashReg_Click);
            mnuChecklist.Click += new EventHandler(mnuChecklist_Click);
            
            
            mnuExpensesList.Click += new EventHandler(mnuExpensesList_Click);
            
           // mnuChargeInvoiceViewer.Click += new EventHandler(mnuChargeInvoiceViewer_Click);
            mnuStatementOfAccount.Click += new EventHandler(mnuStatementOfAccount_Click);
            
            
            mnuUpload.Click += new EventHandler(mnuUpload_Click);
            
            mnuStockLogViewer.Click += new EventHandler(mnuStockLogViewer_Click);
            mnuStockInventoryViewer.Click += new EventHandler(mnuStockInventoryViewer_Click);

            chargeSummary.Click += new EventHandler(chargeSummary_Click);
            //MainSlider
            sldMain.ItemAdded += new EventHandler(sldMain_ItemAdded);
            sldMain.ItemRemoved += new EventHandler(sldMain_ItemRemoved);
            sldMain.AnimationCompleted += new EventHandler(sldMain_AnimationCompleted);

            mnuPurchaseOrderManager.Click += new EventHandler(mnuPurchaseOrderManager_Click);
        }

        void mnuPurchaseOrderManager_Click(object sender, EventArgs e)
        {
            frmPurchaseOrderManager frm = new frmPurchaseOrderManager();
            frm.Initialize("Purchase Order Summary");
            sldMain.AddItem(frm.PanelForm, "Purchase Order Summary");
        }

        void chargeSummary_Click(object sender, EventArgs e)
        {
           // frmChargeSummary frm = new frmChargeSummary();
           
            frmNewReceivable frm = new frmNewReceivable();
            frm.Initialize("Charged Accounts Summary");
            sldMain.AddItem(frm.PanelForm, "Charged Accounts Summary");
        }

        void mnuStockInventoryViewer_Click(object sender, EventArgs e)
        {
             
            frmInventoryViewer frm = new frmInventoryViewer();
            frm.Initialize("Stock Inventory Viewer");
            sldMain.AddItem(frm.PanelForm, "Stock Inventory Viewer");
        }

        void mnuStockLogViewer_Click(object sender, EventArgs e)
        {
             
            frmLogViewer frm = new frmLogViewer();
            frm.Initialize("Stock Log Viewer");
            sldMain.AddItem(frm.PanelForm, "Stock Log Viewer");
        }
        
        void mnuUpload_Click(object sender, EventArgs e)
        {
             
            frmUpload frm = new frmUpload();
            frm.Initialize("Download Data Online");
            sldMain.AddItem(frm.PanelForm, "Upload Data Online");
        }

        void mnuStatementOfAccount_Click(object sender, EventArgs e)
        {
             
            //frmStatementOfAccount frm = new frmStatementOfAccount();
            frmStatementOfAccountViewer frm = new frmStatementOfAccountViewer();
            frm.Initialize("Statement Of Account Report");
            sldMain.AddItem(frm.PanelForm, "Statent of Account Report");
        }

        void mnuChargeInvoiceViewer_Click(object sender, EventArgs e)
        {
             
            frmChargeInvoiceViewer frm = new frmChargeInvoiceViewer();
            frm.Initialize("Charge Invoice Report");
            sldMain.AddItem(frm.PanelForm, "Charge Invoice Report");  
        }

        

        void mnuExpensesList_Click(object sender, EventArgs e)
        {
             
            frmExpensesList frm = new frmExpensesList();
            frm.Initialize("Expenses List");
            sldMain.AddItem(frm.PanelForm, "Expenses List");
        }
        
        void mnuChecklist_Click(object sender, EventArgs e)
        {
             
            frmCheckList frm = new frmCheckList();
            frm.Initialize("Check List");
            sldMain.AddItem(frm.PanelForm, "Check List");
        }

        void mnuCashReg_Click(object sender, EventArgs e)
        {
             
            frmCashRegisteredList frm = new frmCashRegisteredList();
            frm.Initialize("Cash Registered List");
            sldMain.AddItem(frm.PanelForm, "Cash Registered List");
        }
        
        void mnuAboutThis_Click(object sender, EventArgs e)
        {
            frmAboutThis frm = new frmAboutThis();
            frm.Initialize("About This");
            frm.FrameSliderVersion(sldMain.Version);
            sldMain.AddItem(frm.PanelForm, "About This");
        }
        
        void mnuDailySales_Click(object sender, EventArgs e)
        {
            if (mnuSales.Enabled)
            {
                frmDailySales frm = new frmDailySales();
                frm.Initialize("Daily Sales Report");
                sldMain.AddItem(frm.PanelForm, "Daily Sales Report");
            }
        }

        void mnuViewPriceList_Click(object sender, EventArgs e)
        {
            frmPriceListViewer frm = new frmPriceListViewer();
            frm.Initialize("Price List Viewer");
            sldMain.AddItem(frm.PanelForm, "Price List Viewer");
        }
        
        void mnuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        void mnuSettings_Click(object sender, EventArgs e)
        {
            frmSettings frm = new frmSettings();
            frm.Initialize("Connection Settings");
            sldMain.AddItem(frm.PanelForm, "Connection Settings");
        }
        
        void mnuCloseAll_Click(object sender, EventArgs e)
        {
           
            if (sldMain.ItemCount > 0)
            {
                DialogResult dlg = MessageBox.Show("This will close all session. Are you sure?", "Close Sessions", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlg == DialogResult.Yes)
                {
                    sldMain.Controls.Clear();
                }
            }
        }

        void sldMain_AnimationCompleted(object sender, EventArgs e)
        {
            for (int i = 0; i < mnuSession.DropDownItems.Count; i++)
            {
                mnuSession.DropDownItems[i].ForeColor = Color.Black;
            }
            mnuSession.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
        }
        void sldMain_ItemRemoved(object sender, EventArgs e)
        {
            string[] buf = sldMain.ItemList;
            mnuSession.DropDownItems.Clear();
            for (int i = 0; i < buf.Length; i++)
            {
                mnuSession.DropDownItems.Add(i + " - " + buf[i], null, new EventHandler(Session_Click));
                mnuSession.DropDownItems[i].ForeColor = Color.Black;
            }
            if(sldMain.ItemCount > 0)mnuSession.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
            mnuCloseAll.Text = "Close All Sessions (" + buf.Length + ")";
        }
        void sldMain_ItemAdded(object sender, EventArgs e)
        {
            string[] buf = sldMain.ItemList;
            mnuSession.DropDownItems.Clear();
            for (int i = 0; i < buf.Length; i++)
            {
                mnuSession.DropDownItems.Add(i + " - " + buf[i], null, new EventHandler(Session_Click));
                mnuSession.DropDownItems[i].ForeColor = Color.Black;
            }
            mnuSession.DropDownItems[sldMain.SelectedIndex].ForeColor = Color.RoyalBlue;
            mnuCloseAll.Text = "Close All Sessions (" + buf.Length + ")";
        }
        void Session_Click(object sender, EventArgs e)
        {
            int index = mnuSession.DropDownItems.IndexOf((ToolStripItem)sender);
            for (int i = 0; i < mnuSession.DropDownItems.Count; i++)
            {
                mnuSession.DropDownItems[i].ForeColor = Color.Black;
            }
            sldMain.MoveTo(index);
            mnuSession.DropDownItems[index].ForeColor = Color.RoyalBlue;
        }

        void frmMain_Load(object sender, EventArgs e)
        {
            this.Width = Screen.PrimaryScreen.Bounds.Width;
            this.Height = Screen.PrimaryScreen.Bounds.Height;
            this.Left = Screen.PrimaryScreen.Bounds.X;
            this.Top = Screen.PrimaryScreen.Bounds.Y;
            frmMain_Resize(sender, e);
            Common.parent = this;
            LoadSettings();
            
            
        }
        void frmMain_Resize(object sender, EventArgs e)
        {
            sldMain.Left = 12;
            sldMain.Width = this.Width - sldMain.Left - 13;
            sldMain.Height = this.Height - sldMain.Top - 32;
        }
        void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dlg = MessageBox.Show("Are you sure to exit the application?",
               "Exit Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
            if (dlg == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        //Methods
        void LoadSettings()
        {
            uTesselation tes = new uTesselation("lowgate", 256);
            SettingType[] st = new SettingType[0];
            try
            {
                st = Common.ReadSettings();

                Common.lServer = st[0].Node[0].Value;
                Common.lDatabase = st[0].Node[1].Value;
                Common.lUID = st[0].Node[2].Value;
                Common.lPWD = tes.Tesselate(st[0].Node[3].Value);

                Common.lStoreID = st[1].Node[0].Value;
                Common.lStoreName = st[1].Node[1].Value;
                Common.lStoreAddress = st[1].Node[2].Value;

                Common.oServer = st[2].Node[0].Value;
                Common.oDatabase = st[2].Node[1].Value;
                Common.oUID = st[2].Node[2].Value;
                Common.oPWD = tes.Tesselate(st[2].Node[3].Value);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot read settings");
            }
        }

        public void ViewPO(string number, string storeName)
        {
            frmPurchaseOrderEntry frm = new frmPurchaseOrderEntry(number,storeName);
            frm.Initialize("Purchase Order #" + number);
            sldMain.AddItem(frm.PanelForm, "Purchase Order #" + number);
        }

        public void ViewChargedInvoice(string number, string storeName)
        {
            frmChargedInvoiceViewer frm = new frmChargedInvoiceViewer(number, storeName);
            frm.Initialize("Charge Invoice #" + number);
            sldMain.AddItem(frm.PanelForm, "Charge Invoice #" + number);
        }
    }
}
